﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Resources;
using System.Reflection;
using System.IO;

namespace TestGen
{
    /// <summary>
    /// Helper test class to provide a list of fen and perft info for testing
    /// </summary>
    internal static class PerftTestSuite
    {
        /// <summary>
        /// Retrieve a list of test cases from resource
        /// </summary>
        /// <returns>a list of PerfTestItem to check</returns>
        public static ICollection<PerftTestItem> GetAll()
        {
            List<PerftTestItem> items = new List<PerftTestItem>();
            StreamReader sr = new StreamReader(
                            Assembly
                            .GetExecutingAssembly()
                            .GetManifestResourceStream("TestGen.PerfTestSuite.epd"));
            string line = "";
            while ( null != (line = sr.ReadLine()))
            {
                line = line.Trim();
                if (string.IsNullOrEmpty(line))
                    continue;
                string[] tokens = line.Split(';');
                PerftTestItem item = new PerftTestItem();
                item.Fen = tokens[0].Trim();
                item.Expectations = BuildExpectations(tokens);
                items.Add(item);
            }
            return items;
        }

        private static PerftExpectation[] BuildExpectations(string[] tokens)
        {
            List<PerftExpectation> res = new List<PerftExpectation>();
            for (int i = 1; i < tokens.Length; ++i)
            {
                string perft = tokens[i].Trim();
                string[] depthNodes = perft.Split(' ');
                if (depthNodes.Length == 2)
                {
                    string depth = depthNodes[0].Trim();
                    string nodes = depthNodes[1].Trim();
                    if (depth[0] == 'D')
                    {
                        PerftExpectation pe = new PerftExpectation();
                        pe.Depth = int.Parse(depth.Substring(1));
                        pe.Count = ulong.Parse(nodes);
                        res.Add(pe);
                    }
                }
            }
            return res.ToArray();
        }
    }
}
